package org.future.admin;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;

import org.future.server.ConfigUtil;

public class ShutDown {
	private String host = "localhost";
	private int port = ConfigUtil.getInt("srv_shut_port");
	private int timeout = 1000*60*3;
	private BufferedReader in;
	private PrintWriter out;
	
	private void shutdown(){
		Socket socket = null;
		try{
			socket = new Socket(host,port);
			socket.setSoTimeout(timeout);
			// create in and out
			in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
			out = new PrintWriter(socket.getOutputStream());
			// send the shutdown command
			out.println("shutdown");
			out.flush();
			// get the backinfo
			String msg = null;
			while((msg = in.readLine()) != null){
				System.out.println(msg);
			}
		} catch(IOException e){
			e.printStackTrace();
		} finally{
			try{
				if(socket != null){
					socket.close();
				}
			} catch(IOException e){
			}
		}
	}
	
	public static void main(String[] args) {
	    ShutDown shutdown = new ShutDown();
	    shutdown.shutdown();
    }
}
